//
//  BaseTabBar.m
//  RadioBroadcastingStation
//
//  Created by ytkjs2 on 2019/5/7.
//  Copyright © 2019年 ytk. All rights reserved.
//

#import "BaseTabBar.h"

#define tabbarHeihgt 49

@interface BaseTabBar ()

@property (nonatomic,weak) UIButton *centerBtn;

@end

@implementation BaseTabBar

-(UIButton *)centerBtn{
    
    if (!_centerBtn) {
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"icon_player_round"] forState:UIControlStateNormal];
        [btn sizeToFit];
        [self addSubview:btn];
        _centerBtn =btn;
//        [self setPlayerButtonRotating:btn];
        [btn addTarget:self action:@selector(pushView) forControlEvents:UIControlEventTouchUpInside];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setPlayerButtonRotating) name:@"playerRun" object:nil];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopPlayerButtonRotating) name:@"playerStop" object:nil];
        
    }
    return _centerBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width =self.bounds.size.width/(self.items.count +1);
    CGFloat height =tabbarHeihgt;
    CGFloat x =0;
    int index =0;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (index ==2) {
                index +=1;
            }
            view.frame =CGRectMake(x + width *index, 0, width, height);
            index ++;
        }
    }
    
    self.centerBtn.center =CGPointMake(self.bounds.size.width *0.5, height *0.5);
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.clipsToBounds || self.hidden || (self.alpha == 0.f)) {
        return nil;
    }
    UIView *result = [super hitTest:point withEvent:event];
    // 如果事件发生在 tabbar 里面直接返回
    if (result) {
        return result;
    }
    // 这里遍历那些超出的部分就可以了，不过这么写比较通用。
    for (UIView *subview in self.subviews) {
        // 把这个坐标从tabbar的坐标系转为 subview 的坐标系
        CGPoint subPoint = [subview convertPoint:point fromView:self];
        result = [subview hitTest:subPoint withEvent:event];
        // 如果事件发生在 subView 里就返回
        if (result) {
            return result;
        }
    }
    return nil;
}

#pragma mark --中间按钮旋转核心代码
-(void)setPlayerButtonRotating
{
    CABasicAnimation *animation = [ CABasicAnimation animationWithKeyPath: @"transform" ];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //围绕Z轴旋转，垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI/2.0, 0.0, 0.0, 1.0) ];
    animation.duration = 3;
    //旋转效果累计，先转180度，接着再旋转180度，从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    [self.centerBtn.layer addAnimation:animation forKey:nil];
}

-(void)stopPlayerButtonRotating
{
    [self.centerBtn.layer removeAllAnimations];
}

-(void)pushView
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"PushPlayer" object:nil];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"PushPlayer1" object:nil];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"PushPlayer2" object:nil];
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"PushPlayer3" object:nil];
}

@end
